﻿using MQTTnet;
using MQTTnet.Protocol;
using MQTTnet.Server;
using SmartAgricultureService.Models;
using System.Text;

namespace SmartAgricultureService.Core.Mqtt
{
    public sealed class MqttService
    {
        public const string ServerClientId = "SAS_SERVER";
        public static MqttServer Server { get; internal set; }
        public static async Task PublishData(MqttData mqttData)
        {
            var data = Encoding.UTF8.GetBytes(mqttData.ToString());
            var message = new MqttApplicationMessage
            {
                Retain = true,
                PayloadSegment = data,
                Topic = AppConfiguration.Mqtt.Topic,
                QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
            };
            await Server.InjectApplicationMessage(new InjectedMqttApplicationMessage(message)
            { SenderClientId = ServerClientId });
        }
    }
}